package com.founder.bdyx.modules.sys.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.founder.bdyx.core.entity.ProcessResult;
import com.founder.bdyx.core.result.PageResult;
import com.founder.bdyx.core.util.DictConvertUtil;
import com.founder.bdyx.modules.sys.model.ApplyInfo;
import com.founder.bdyx.modules.sys.service.ApplyInfoService;
import com.founder.bdyx.wxpro.conf.WxConfig;
import com.founder.bdyx.wxpro.util.HttpUtil;
import com.founder.bdyx.wxpro.util.VerifyUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

import static com.founder.bdyx.core.entity.ProcessResult.ERROR;

@RestController
@RequestMapping("/sys/finish")
public class FinishController {
    Log log = LogFactory.getLog(FinishController.class);

    @Autowired
    private ApplyInfoService applyInfoService;
    @Autowired
    private WxConfig wxConfig;

    @ApiOperation(value = "审批列表视图",notes = "审批列表视图")
    @GetMapping("/list")
    public ModelAndView list() {
        return new ModelAndView("/modules/finish/list");
    }

    @ApiOperation(value = "获取审核列表",notes = "获取审核列表")
    @RequestMapping(value = "",method = RequestMethod.GET)
    public List<ApplyInfo> getAll(@ApiParam(name = "applyInfo",value="审批实体",required = true) ApplyInfo applyInfo,
                                        @ApiParam(name = "keyword",value="查询字段",required = false) String keyword,
                                        @ApiParam(name = "transType",value="查询字段",required = false) String transType) {
        List<ApplyInfo> applyInfos = applyInfoService.getAllForFinish(applyInfo,keyword,transType);
        if(applyInfos != null && applyInfos.size()>0)
            DictConvertUtil.converter(applyInfos);
//        return new PageResult(new PageInfo<ApplyInfo>(applyInfos));
        return applyInfos;
    }

    @ApiOperation(value = "申请详情视图",notes = "申请详情视图")
    @RequestMapping(value = "/form",method = RequestMethod.GET)
    public ModelAndView form() {
        return new ModelAndView("/modules/finish/form");
    }

    @RequestMapping(value = "/writeBackForm",method = RequestMethod.GET)
    public ModelAndView writeBackForm() {
        return new ModelAndView("/modules/finish/writeBack");
    }

    @RequestMapping(value = "/pickupForm",method = RequestMethod.GET)
    public ModelAndView pickupForm() {
        return new ModelAndView("/modules/finish/pickup");
    }

    @ApiOperation(value = "获取申请详细信息",notes = "获取申请详细信息")
    @PostMapping(value = "/view/{id}")
    public ApplyInfo view(@ApiParam(name = "id", value = "申请单id", required = true) @PathVariable String id){
        ApplyInfo applyInfo = applyInfoService.getByAppId(id);
        DictConvertUtil.converter(applyInfo);
        return applyInfo;
    }

    @PostMapping(value = "/writeBackNo")
    public JSONObject writeBackNo(String logisticsNo, Integer id){

        int num = applyInfoService.updateLogisticsNo(logisticsNo,id);
        JSONObject obj = new JSONObject();
        if(num>0){
            obj.put("code",1);
            obj.put("msg","回填单号成功！");
        }else{
            obj.put("code",-1);
            obj.put("msg","回填单号失败！");
        }
        return obj;
    }

    @PostMapping(value = "/pickupNo")
    public JSONObject pickupNo(String logisticsNo, Integer id){

        int num = applyInfoService.updatePickUpNo(logisticsNo,id);
        JSONObject obj = new JSONObject();
        if(num>0){
            obj.put("code",1);
            obj.put("msg","验证通过！");
        }else{
            obj.put("code",-1);
            obj.put("msg","验证码不匹配！");
        }
        return obj;
    }
}
